package com.vshop.controller.externalInterface;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.vshop.entity.external.verification.response.ResponseVerification;
import com.vshop.entity.external.verification.response.ResponseVerificationData;
import com.vshop.entity.external.verification.response.ResponseVerificationTradeData;
import com.vshop.entity.requestinterface.Constant;
import com.vshop.entity.requestinterface.userlogin.request.UserLoginRequest;
import com.vshop.entity.requestinterface.userlogin.response.UserLoginResponse;
import com.vshop.service.SequenceService;
import com.vshop.utils.HttpUtil;
import com.vshop.utils.Md5Encrypt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 专门用于外部接口来验证当前ICCID是否属于当前企业，以及验证ICCID的有效性
 *
 * @author lm
 * @date 2019/09/03
 */
@RestController
@Api(description = "验证ICCID")
@Slf4j
public class VerificationController {
    @Autowired
    private SequenceService sequenceService;

    /**
     * 直接请求网关xml接口的地址
     */
    @Value("${iot.requestURI}")
    private String requestURI;

    @GetMapping("verify/{deviceCode}")
    @CrossOrigin
    @ResponseBody
    @ApiOperation(value = "验证ICCID", response = ResponseVerification.class)

    public String verify(@PathVariable(value = "deviceCode") String deviceCode) {

        // 1，生成流水号
        String connSeqNo = sequenceService.getPackageTaskNo();

        // 2,生成当前时间戳
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curtime = df.format(new Date());

        //先判断链接中deviceCode有没有

        if (StringUtils.isBlank(deviceCode)) {

            ResponseVerificationData data = new ResponseVerificationData();
            data.setConnSeqNo(connSeqNo);
            data.setTradeType("verify");
            data.setTradeTime(curtime);
            data.setRetCode("0201");
            data.setRetMsg("ICCID不允许空");

            ResponseVerification res = new ResponseVerification();
            res.setData(data);
            res.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));
            String jsonString = JSON.toJSONString(res);
            log.info("响应报文：" + jsonString);
            return jsonString;

        } else {
            //1,先去北京的接口查询当前的渠道编码是多少 :用deviceCode去iot网关查询企业编码

            UserLoginRequest user = new UserLoginRequest();
            user.setDeviceId(deviceCode);
            //组装需要的json去请求报文
            String requestInterface = JSON.toJSONString(user);
            log.info("请求北京的接口请求报文：" + requestInterface);
            String url = requestURI + Constant.iot_queryPartner_2;
            // 发送post请求，并接收返回结果
            String returnStr = HttpUtil.httpPost(url, requestInterface);
            log.info("北京回复的下发接口响应报文：" + returnStr);
            UserLoginResponse response = JSONArray.parseObject(returnStr, UserLoginResponse.class);

            ResponseVerificationTradeData tradeData = null;

            if (response != null) {
                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(response.getRetCode())) {

                    if (response.getPartnerCode() != null) {

                        tradeData = new ResponseVerificationTradeData();
                        tradeData.setPartnerCode(response.getPartnerCode());

                    }

                    ResponseVerificationData data = new ResponseVerificationData();
                    data.setConnSeqNo(connSeqNo);
                    data.setTradeType("verify");
                    data.setTradeTime(curtime);
                    data.setRetCode(response.getRetCode());
                    data.setRetMsg(response.getResultInfo());
                    data.setTradeData(tradeData);

                    ResponseVerification res = new ResponseVerification();
                    res.setData(data);
                    res.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));
                    String jsonString = JSON.toJSONString(res);
                    log.info("响应报文：" + jsonString);
                    return jsonString;

                } else {

                    ResponseVerificationData data = new ResponseVerificationData();
                    data.setConnSeqNo(connSeqNo);
                    data.setTradeType("verify");
                    data.setTradeTime(curtime);
                    data.setRetCode("0202");
                    data.setRetMsg("无效的ICCID");

                    ResponseVerification res = new ResponseVerification();
                    res.setData(data);
                    res.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));
                    String jsonString = JSON.toJSONString(res);
                    log.info("响应报文：" + jsonString);
                    return jsonString;


                }

            } else {

                ResponseVerificationData data = new ResponseVerificationData();
                data.setConnSeqNo(connSeqNo);
                data.setTradeType("verify");
                data.setTradeTime(curtime);
                data.setRetCode("9999");
                data.setRetMsg("接口响应出现异常");

                ResponseVerification res = new ResponseVerification();
                res.setData(data);
                res.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));
                String jsonString = JSON.toJSONString(res);
                log.info("响应报文：" + jsonString);
                return jsonString;


            }


        }
    }

}
